package com.example.demo.block;

import com.example.demo.Demo;
import com.example.demo.block.custom.FireEtherBlock;
import com.example.demo.constant.BlockConstant;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.ExperienceDroppingBlock;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.intprovider.UniformIntProvider;

public class ModBlocks {

    public static final Block EXAMPLE_BLOCK = registerModBlocks(BlockConstant.EXAMPLE_NAME, new Block(FabricBlockSettings.copyOf(Blocks.STONE)));
    public static final Block RAW_EXAMPLE_BLOCK = registerModBlocks(BlockConstant.RAW_EXAMPLE_NAME, new Block(FabricBlockSettings.copyOf(Blocks.STONE)));
    public static final Block EXAMPLE_ORE_BLOCK = registerModBlocks(BlockConstant.EXAMPLE_ORE_NAME,
            new ExperienceDroppingBlock(FabricBlockSettings.copyOf(Blocks.STONE),
                    UniformIntProvider.create(2, 5)));
    public static final Block FIRE_ETHER = registerModBlocks(BlockConstant.FIRE_ETHER_NAME,
            new FireEtherBlock(FabricBlockSettings.copyOf(Blocks.STONE)));

    /**
     * 在游戏内注册一个模组方块，并同时注册其对应的方块物品
     *
     * @param name 方块的名称，用于唯一标识该方块
     * @param block 要注册的方块实例
     * @return 注册后的方块实例
     *
     * 此方法首先调用registerModBlockItem方法来注册方块物品，以确保方块和物品都能在游戏内正常使用
     * 然后，使用Minecraft的Registry系统来正式注册方块，使其在游戏中可用
     * 通过这种方式，可以确保方块及其物品形式在游戏世界中被正确地识别和使用
     */
    public static Block registerModBlocks(String name, Block block) {
        // 先注册方块物品，确保方块和物品能同步创建和使用
        registerModBlockItems(name, block);

        // 注册方块本身，使其能在游戏世界中被放置和使用
        return Registry.register(Registries.BLOCK, new Identifier(Demo.MOD_ID, name), block);
    }
    public static  Item registerModBlockItems(String name, Block block) {
        return Registry.register(Registries.ITEM,
                new Identifier(Demo.MOD_ID, name),
                new BlockItem(block, new FabricItemSettings()));
    }

    public static void init() {
        System.out.println("Registering ModBlocks for " + Demo.MOD_ID);
    }
}
